/* 时间戳对比 */
export function computationTime (time1, time2) {
  var date1 = new Date(time1)
  var date2 = new Date(time2)
  var s1 = date1.getTime()
  var s2 = date2.getTime()
  var total = (s2 - s1) / 1000
  return total / 60
}

export function myFunction (time) {
  var dateee = new Date(time).toJSON()
  var date = new Date(+new Date(dateee) + 8 * 3600 * 1000)
    .toISOString()
    .replace(/T/g, ' ')
    .replace(/\.[\d]{3}Z/, '')
  return date
}
export function getHalfCheckeds (arr, list, id, newArr) {
  list.map(item => {
    if (item.id === id && item.children) {
      newArr.push(id)
      getHalfCheckeds(arr, item.children, id, newArr)
    } else if (item.id !== id && item.children) {
      getHalfCheckeds(arr, item.children, id, newArr)
    }
  })
}
/**  秒转成 xx天xx小时xx分钟 */
export function formatSeconds (value) {
  var theTime = parseInt(value)// 需要转换的时间秒
  var theTime1 = 0// 分
  var theTime2 = 0// 小时
  var theTime3 = 0// 天
  if (theTime > 60) {
    theTime1 = parseInt(theTime / 60)
    theTime = parseInt(theTime % 60)
    if (theTime1 > 60) {
      theTime2 = parseInt(theTime1 / 60)
      theTime1 = parseInt(theTime1 % 60)
      if (theTime2 > 24) {
        // 大于24小时
        theTime3 = parseInt(theTime2 / 24)
        theTime2 = parseInt(theTime2 % 24)
      }
    }
  }
  var result = ''
  if (theTime > 0) {
    result = '' + parseInt(theTime) + '秒'
  }
  if (theTime1 > 0) {
    result = '' + parseInt(theTime1) + '分' + result
  }
  if (theTime2 > 0) {
    result = '' + parseInt(theTime2) + '小时' + result
  }
  if (theTime3 > 0) {
    result = '' + parseInt(theTime3) + '天' + result
  }
  return result
}
/* 图片放大赋值 */
export function imgBig (imgList) {
  const arr = []
  if (imgList.length > 0) {
    imgList.forEach((i) => {
      arr.push({
        name: i.fileName,
        uid: i.fileId,
        url: i.fileUrl,
        fileUrl: i.fileUrl,
        filePath: i.fileUrl,
        type: i.fileType,
        fileId: i.fileId
      })
    })
  }
  return arr
}
/**  树结构数据转扁平化数据 */
const getNodeMap = (node, parentNode) => {
  node.parentNode = parentNode
  const nodeMap = [node]
  if (node.children && node.children.length) {
    node.children.forEach((item) => nodeMap.push(...getNodeMap(item, node)))
  }
  return nodeMap
}

export const getTreeMap = (tree) => {
  if (!(tree instanceof Array)) return
  const treeMap = []
  tree.forEach((node) => {
    treeMap.push(...getNodeMap(node, tree))
  })
  return treeMap
}
